<?php

namespace app\wechat\controller;

use app\wechat\lib\Wechat;
use EasyWeChat\Factory;

class Message extends Base
{

    /**
     * @param $express_openid 承运方的open_id
     * @param $order_no 订单的id号
     * @param $mailName
     * @param $mailPrice
     * @param $mailTel
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     */
    public function ToExpress($express_openid, $name, $goods_name)
    {
        $config = [
            // 前面的appid什么的也得保留哦
            'app_id'        => Wechat::app_id,
            'mch_id'        => Wechat::mch_id,
            'secret'        => Wechat::secret,
            'response_type' => 'array',
            'key'           => Wechat::key,
            'oauth'         => [
                'scopes' => ['snsapi_userinfo'],
            ],
        ];

        $app    = Factory::officialAccount($config);
        $result = $app->template_message->send([
            'touser'      => $express_openid,
            'template_id' => 'jlE4oaHFV_LHcDafnu2pvPJ_6ODxpXSKUHlQBi7F2ys',
            'url'         => url('wechat/express/offer'),
            'data'        => [
                'first'    => '收到一条流程待办提醒',
                'keyword1' => '货物审批',
                'keyword2' => '报价',
                'keyword3' => $name,
                'keyword4' => $goods_name,
                'keyword5' => date('Y-m-d H:i:s', time()),
                'remark'   => "请您在4小时之内尽快处理",
            ],
        ]);
    }

    /**
     * @param $apply_openid 申请员的open_id
     * @param $name     申请员的姓名
     * @param $goods_name  货物名称
     * @param $company_info 报价方公司名称
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     */
    public function PriceToApply($apply_openid, $name, $goods_name, $priceDataID, $company_info)
    {
        $config = [
            // 前面的appid什么的也得保留哦
            'app_id'        => Wechat::app_id,
            'mch_id'        => Wechat::mch_id,
            'secret'        => Wechat::secret,
            'response_type' => 'array',
            'key'           => Wechat::key,
            'oauth'         => [
                'scopes' => ['snsapi_userinfo'],
            ],
        ];

        $app    = Factory::officialAccount($config);
        $result = $app->template_message->send([
            'touser'      => $apply_openid,
            'template_id' => 'jlE4oaHFV_LHcDafnu2pvPJ_6ODxpXSKUHlQBi7F2ys',
            'url'         => url('wechat/express/select', ['id' => $priceDataID]),
            'data'        => [
                'first'    => '收到一条流程待办提醒',
                'keyword1' => '货物审批',
                'keyword2' => '已报价,请查看',
                'keyword3' => $name,
                'keyword4' => $goods_name,
                'keyword5' => date('Y-m-d H:i:s', time()),
                'remark'   => "报价方为" . $company_info . '.请您尽快处理.',
            ],
        ]);
    }

    /**
     * @param $expredd_openid
     * @param $name
     * @param $goods_name
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     */
    public function UserToExpress($expredd_openid, $name, $goods_name)
    {
        $config = [
            // 前面的appid什么的也得保留哦
            'app_id'        => Wechat::app_id,
            'mch_id'        => Wechat::mch_id,
            'secret'        => Wechat::secret,
            'response_type' => 'array',
            'key'           => Wechat::key,
            'oauth'         => [
                'scopes' => ['snsapi_userinfo'],
            ],
        ];

        $app    = Factory::officialAccount($config);
        $result = $app->template_message->send([
            'touser'      => $expredd_openid,
            'template_id' => 'jlE4oaHFV_LHcDafnu2pvPJ_6ODxpXSKUHlQBi7F2ys',
            'url'         => url('wechat/index/index'),
            'data'        => [
                'first'    => '您好!这批货物由您承运.',
                'keyword1' => '货物审批',
                'keyword2' => '已确定承运方',
                'keyword3' => $name,
                'keyword4' => $goods_name,
                'keyword5' => date('Y-m-d H:i:s', time()),
                'remark'   => "请您尽快处理",
            ],
        ]);
    }

    /**
     * @param $apply_openid 申请员
     * @param $name  姓名
     * @param $goods_name 商品名
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     */
    public function ReceiveToApply($apply_openid, $name, $goods_name)
    {
        $config = [
            // 前面的appid什么的也得保留哦
            'app_id'        => Wechat::app_id,
            'mch_id'        => Wechat::mch_id,
            'secret'        => Wechat::secret,
            'response_type' => 'array',
            'key'           => Wechat::key,
            'oauth'         => [
                'scopes' => ['snsapi_userinfo'],
            ],
        ];
        $app    = Factory::officialAccount($config);
        $result = $app->template_message->send([
            'touser'      => $apply_openid,
            'template_id' => 'jlE4oaHFV_LHcDafnu2pvPJ_6ODxpXSKUHlQBi7F2ys',
            'url'         => url('wechat/index/index'),
            'data'        => [
                'first'    => '订单收货通知.',
                'keyword1' => '货物审批',
                'keyword2' => '已收货',
                'keyword3' => $name,
                'keyword4' => $goods_name,
                'keyword5' => date('Y-m-d H:i:s', time()),
                'remark'   => "请您查看",
            ],
        ]);
    }

    /**
     * @param $apply_openid
     * @param $name
     * @param $goods_name
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     */
    public function SuccessToApply($apply_openid, $name, $goods_name)
    {
        $config = [
            // 前面的appid什么的也得保留哦
            'app_id'        => Wechat::app_id,
            'mch_id'        => Wechat::mch_id,
            'secret'        => Wechat::secret,
            'response_type' => 'array',
            'key'           => Wechat::key,
            'oauth'         => [
                'scopes' => ['snsapi_userinfo'],
            ],
        ];

        $app    = Factory::officialAccount($config);
        $result = $app->template_message->send([
            'touser'      => $apply_openid,
            'template_id' => 'jlE4oaHFV_LHcDafnu2pvPJ_6ODxpXSKUHlQBi7F2ys',
            'url'         => url('wechat/index/index'),
            'data'        => [
                'first'    => '您好.您的订单审核通过',
                'keyword1' => '货物审批',
                'keyword2' => '已审批',
                'keyword3' => $name,
                'keyword4' => $goods_name,
                'keyword5' => date('Y-m-d H:i:s', time()),
                'remark'   => "请您查看",
            ],
        ]);
    }

    /**
     * @param $apply_openid
     * @param $name
     * @param $goods_name
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     */
    public function RefuseToApply($apply_openid, $name, $goods_name)
    {
        $config = [
            // 前面的appid什么的也得保留哦
            'app_id'        => Wechat::app_id,
            'mch_id'        => Wechat::mch_id,
            'secret'        => Wechat::secret,
            'response_type' => 'array',
            'key'           => Wechat::key,
            'oauth'         => [
                'scopes' => ['snsapi_userinfo'],
            ],
        ];

        $app    = Factory::officialAccount($config);
        $result = $app->template_message->send([
            'touser'      => $apply_openid,
            'template_id' => 'jlE4oaHFV_LHcDafnu2pvPJ_6ODxpXSKUHlQBi7F2ys',
            'url'         => url('wechat/index/index'),
            'data'        => [
                'first'    => '您好.您的订单未审核通过',
                'keyword1' => '货物审批',
                'keyword2' => '未通过审核',
                'keyword3' => $name,
                'keyword4' => $goods_name,
                'keyword5' => date('Y-m-d H:i:s', time()),
                'remark'   => "请您查看",
            ],
        ]);
    }
}
